<?php


namespace App\Common\Utill;


class AppVersionUtil
{
    /**
     * 转数字
     * @param string $version
     * @return int
     */
    public static function toInt(string $version): int
    {
        $slice = explode('.', $version);
        $segment1 = $segment2 = $segment3 = 0;
        if (isset($slice[0])) {
            $segment1 = intval($slice[0]) * 10000;
        }
        if (isset($slice[1])) {
            $segment2 = intval($slice[1]) * 100;
        }
        if (isset($slice[2])) {
            $segment3 = intval($slice[2]);
        }
        return $segment1 + $segment2 + $segment3;
    }

    /**
     * 转字符
     * @param int $version
     * @return string
     */
    public static function toString(int $version): string
    {
        $segment1 = intdiv($version, 10000);
        $version -= $segment1 * 10000;
        $segment2 = intdiv($version, 100);
        $version -= $segment2 * 100;
        return sprintf('%d.%d.%d', $segment1, $segment2, $version);
    }
}
